package PrimaryMath;

import java.util.ArrayList;

import java.util.Scanner;

public class PrimaryMath {
    static int score = 0;

    static String grade;

    static ArrayList<Object> dataOfLast = new ArrayList<>();

    static ArrayList<Object> dataAll = new ArrayList<>();

    public static void main(String[] args) {
        System.out.println("******欢迎使用*******");
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("****请输入您的ID号****");
            String ID = sc.next();
            if (judge(ID) == 1) {
                while (true) {
                    int choice = menu();
                    choose(choice, ID);
                }
            }
        }
    }
    /**
     * 判断ID是否合法
     */
    public static int judge(String ID) {
        int letter = 0;
        int num = 0;
        char[] newID = ID.toCharArray();
        for (char c : newID) {
            if (c >= 'A' && c <= 'Z') {
                letter++;
            } else if (c >= '0' && c <= '9') {
                num++;
            }
        }
        if (letter == 2 && num == 4) {
            System.out.println("****即将进入菜单****");
            return 1;
        } else {
            System.out.println("输入的ID号不合法，请重新输入");
        }
        return 2;
    }
    /**
     * 打印菜单
     */
    public static int menu() {
        System.out.println("*******欢迎*******");
        System.out.println("****1.测试计算*****");
        System.out.println("***2.本次测试数据***");
        System.out.println("***3.历史数据打印***");
        System.out.println("******4.退出******");
        System.out.println("***请输入你的选择***");
        Scanner sc = new Scanner(System.in);
        return sc.nextInt();
    }
    /**
     * 选择功能与跳转
     */
    public static void choose(int choice, String ID) {
        switch (choice) {
            case 1:
                long startTime = System.currentTimeMillis();
                dataOfLast.clear();
                test();
                long endTime = System.currentTimeMillis();
                long totalTime = endTime - startTime;
                recordOfLast(totalTime, ID);
                recordAll(totalTime, ID);
                score = 0;
                break;
            case 2:
                if(dataOfLast.size() == 0){
                    System.out.println("本次测试还未开始，暂无本次测试数据！");
                }
                else {
                    System.out.println("下面打印了这次测试的ID，得分，时间，等级");
                    System.out.println(dataOfLast);
                }
                break;
            case 3:
                if(dataAll.size() == 0){
                    System.out.println("本次测试还未开始，暂无本次测试的所有历史记录！");
                }
                else {
                    System.out.println("下面打印了历史测试的ID，得分，时间，等级");
                    System.out.println(dataAll);
                }
                break;
            case 4:
                System.out.println("退出成功，欢迎下次再来！");
                System.exit(0);
            default:
                System.out.println("错误，请重新选择");
                break;
        }
    }
    /**
     * 按照要求生成不同运算符
     */
    public static void test() {
        int flag = 0;
        int flag1 = 0;
        int flag2 = 0;
        for (int i = 0; i < 10; i++) {
            int operator = (int) (Math.random() * 2 + 1);
            while (flag == operator) {
                operator = (int) (Math.random() * 2 + 1);
            }
            switch (operator) {
                case 1:
                    int operator1 = (int) (Math.random() * 2 + 1);
                    while (flag1 == operator1) {
                        operator1 = (int) (Math.random() * 2 + 1);
                    }
                    switch (operator1) {
                        case 1:
                            add();
                            break;
                        case 2:
                            sub();
                            break;
                    }
                    flag1 = operator1;
                    break;
                case 2:
                    int operator2 = (int) (Math.random() * 2 + 1);
                    while (flag2 == operator2) {
                        operator2 = (int) (Math.random() * 2 + 1);
                    }
                    switch (operator2) {
                        case 1:
                            mul();
                            break;
                        case 2:
                            div();
                            break;
                    }
                    flag2 = operator2;
                    break;
            }
            flag = operator;
        }
    }
    /**
     * 加法运算
     */
    public static void add() {
        int num1 = (int) (Math.random() * 50 + 1);
        int num2 = (int) (Math.random() * 50 + 1);
        System.out.println(num1 + "+" + num2 + "=");
        Scanner sc = new Scanner(System.in);
        int answer = sc.nextInt();
        if (answer == num1 + num2) {
            score += 10;
        }
    }
    /**
     * 减法运算
     */
    public static void sub() {
        int num1 = (int) (Math.random() * 100 + 1);
        int num2 = (int) (Math.random() * 100 + 1);
        int temp;
        if (num1 < num2) {
            temp = num1;
            num1 = num2;
            num2 = temp;
        }
        System.out.println(num1 + "-" + num2 + "=");
        Scanner sc = new Scanner(System.in);
        int answer = sc.nextInt();
        if (answer == num1 - num2) {
            score += 10;
        }
    }
    /**
     * 乘法运算
     */
    public static void mul() {
        int num1 = (int) (Math.random() * 10 + 1);
        int num2 = (int) (Math.random() * 10 + 1);
        System.out.println(num1 + "*" + num2 + "=");
        Scanner sc = new Scanner(System.in);
        int answer = sc.nextInt();
        if (answer == num1 * num2) {
            score += 10;
        }
    }
    /**
     * 除法运算
     */
    public static void div() {
        int num1 = (int) (Math.random() * 10 + 1);
        int num2 = (int) (Math.random() * 10 + 1);
        num1 = num1 * num2;
        System.out.println(num1 + "/" + num2 + "=");
        Scanner sc = new Scanner(System.in);
        int answer = sc.nextInt();
        if (answer == num1 / num2) {
            score += 10;
        }
    }
    /**
     * 本次测试数据记录
     */
    public static void recordOfLast(long totalTime, String ID){
        if (score >= 100) {
            grade = "优";
        } else if (score >= 60) {
            grade = "良";
        } else {
            grade = "差";
        }
        dataOfLast.add(ID);
        dataOfLast.add(score);
        dataOfLast.add(totalTime/1000 + "秒");
        dataOfLast.add(grade);
    }
    /**
     * 本次历史数据记录
     */
    public static void recordAll(long totalTime, String ID){
        if (score >= 100) {
            grade = "优";
        } else if (score >= 60) {
            grade = "良";
        } else {
            grade = "差";
        }
        dataAll.add(ID);
        dataAll.add(score);
        dataAll.add(totalTime/1000);
        dataAll.add(grade);
    }
}

